﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using Data.Models;

namespace Parafarmacia
{
    public partial class ControlPanel : System.Web.UI.Page
    {
        /// <summary>
        /// WebForm Load
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
            }
        }

        protected void GridViewContenido_DataBound(object sender, EventArgs e)
        {

        }

        protected void GridViewContenido_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Editar")
            {
                using (var db = new dbParafarmaciaContext())
                {
                    int idx = int.Parse(e.CommandArgument.ToString());
                    int id = int.Parse(GridViewContenido.Rows[idx].Cells[0].Text);
                    var query = from c in db.tblContenidos where c.Id == id select c;

                    tblContenido cont = query.First();

                    txtControlID.Text = cont.NombreControl;
                    txtNombrePagina.Text = cont.NombrePagina;
                    txtDescripcion.Text = cont.Descripcion;
                    txtTexto.Text = cont.Texto;
                    ViewState["ContenidoID"] = id;
                    butEditar.Enabled = true;
                }
            }
        }

        /// <summary>
        /// Actualizar contenido.
        /// </summary>
        private void Update()
        {
            using (var db = new dbParafarmaciaContext())
            {
                int id = int.Parse(ViewState["ContenidoID"].ToString());

                var query = from c in db.tblContenidos where c.Id == id select c;

                tblContenido cont = query.First();

                cont.NombreControl = txtControlID.Text;
                cont.NombrePagina = txtNombrePagina.Text;
                cont.Descripcion = txtDescripcion.Text;
                cont.Texto = txtTexto.Text;

                db.SaveChanges();
                butEditar.Enabled = false;
            }
        }

        /// <summary>
        /// Validar cambios
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        protected void butEditar_Click(object sender, EventArgs e)
        {
            Update();
        }
    }
}